package com.yimi.appstore.api;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.yimi.appstore.db.AppInfo;
import com.yimi.appstore.util.AboutMobileInfoTools;
import com.yimi.appstore.util.AppException;
import com.yimi.appstore.util.SharedPrefsUtil;
import com.yimi.appstore.util.Util;

public class ApiClient {
	
	private static final String TAG = "ApiClient";
	
	
	/**
	 * 用户登录url
	 */
	private static final String LOING_URL = Url.HOST + "yms/rs/platform/start";
	
	/**查询app升级url
	 * 
	 */
	private static final String QUERYAPP_UPDATE_URL = Url.HOST + "/rs/app/upgrade_list";

	interface RESULTKEY{
		String 	SUCCESS = "success";
		String MSG = "msg";
		String UID = "UID";
	}
	
	/**
	 * 用户登录
	 * @param appContext
	 * @return true 成功  false 失败
	 */
	public boolean Login(Context appContext) {
		AboutMobileInfoTools info = AboutMobileInfoTools.getInstance(appContext);
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("csId", 123);
		params.put("imei", info.getImei());
		params.put("mac", info.getLocalMac());
		params.put("netType", info.getNetworkType());
		params.put("osAddtional", 0);
		params.put("osVersion", info.getSDKVersion());
		params.put("packageName", info.getPckName());
		params.put("phoneModel", info.getDeviceType());
		params.put("screenDensity", info.getScreenDensity());
		params.put("screenHeight", info.getScreenHeight());
		params.put("screenWidth", info.getScreenWidth());
		params.put("serviceSupplier", info.getOperator());
		params.put("smsc", "10086");
		params.put("versionCode", info.getVersionCode());
		String result = "";
		boolean success = false;
		String msg = "";
		int uid = 0;
		try {
			result = HttpClientUtil._post(appContext, LOING_URL, params, null);
			try {
				JSONObject object = new JSONObject(result);
				if (object.get(RESULTKEY.SUCCESS) != null) {
					success = object.getBoolean(RESULTKEY.SUCCESS);
				}
				if (object.get(RESULTKEY.MSG) != null) {
					msg = object.getString(RESULTKEY.MSG);
				}
				if (object.get(RESULTKEY.UID) != null) {
					uid = object.getInt(RESULTKEY.UID);
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(success){
			int oldUid = SharedPrefsUtil.getValue(appContext, SharedPrefsUtil.KEY_UID, 0);
			if(uid != oldUid){
				SharedPrefsUtil.putValue(appContext, SharedPrefsUtil.KEY_UID, uid);
				Log.d(TAG, "new uid is "+ uid);
			}
			return true;
		} else {
			
			return false ;
		}
	}
	
	
	public void getAppUpdateList(Context ctx){
		String result = "";
		String requestUrl = "";
		List<AppInfo> appInfos = Util.getAppInfo(ctx);
		HashMap<String, Object> params = new HashMap<String, Object>();
		List<HashMap<String, Object>> appList = new  ArrayList<HashMap<String,Object>>();
		for(AppInfo app : appInfos){
			HashMap< String, Object> map = new HashMap<String, Object>();
			map.put("packageName", app.getPackageName());
			map.put("versionCode", Integer.valueOf(app.getVersionCode()));
			appList.add(map);
		}
		params.put("upgradeList", appList);
		
		try {
			requestUrl = QUERYAPP_UPDATE_URL + "?UID="+SharedPrefsUtil.getValue(ctx, SharedPrefsUtil.KEY_UID, 0);
			result = HttpClientUtil._post(ctx, requestUrl, params, null);
		} catch (AppException e) {
			e.printStackTrace();
		}
	}

}
